package com.zhongmai.yzfgrabanorder.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.text.TextUtils;


import com.zhongmai.yzfgrabanorder.utils.Constants;
import com.zhongmai.yzfgrabanorder.utils.LogUtils;
import com.zhongmai.yzfgrabanorder.utils.LogcatFileManager;
import com.zhongmai.yzfgrabanorder.utils.Utils;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luohao on 2017/2/21 0021.
 * App相关辅助类
 */
public class MyApplication extends Application {
    private static List<Activity> activity_List = new ArrayList<Activity>();
    private static MyApplication appContext;
    public static String Loging_UserName = null;
    private static int mMainThreadId;
    private static Handler mHandler;
    private static int mainThreadId = (int) Thread.currentThread().getId();
    private Handler handler1;

    // 用单例模式保证MyApplication对象在这个程序中只存在一个
    //得到MyApplication对象的方法，并返回MyApplication对象

    /**
     * 全局context方法
     *
     * @return myContext
     */
    public static MyApplication getContext() {
        return appContext;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        if (appContext == null) {
            appContext = MyApplication.this;//创建MyApplication对象
        }
        mHandler = new Handler();
        x.Ext.init(this);
        x.Ext.setDebug(true); //输出debug日志，开启会影响性能
        LogcatFileManager.getInstance().startLogcatManager(getApplicationContext());
        String url = Utils.getValueFromSP(getApplicationContext(), Constants.sp_File, Constants.urlSPKey);
        if (!TextUtils.isEmpty(url)) {
            Constants.Url = url;
            LogUtils.debug("Url===>>>" + Constants.Url, 6);
        }
    }



    /**
     * 返回主线程的pid
     *
     * @return
     */
    public static int getMainThreadId() {
        return mMainThreadId;
    }

    /**
     * 返回Handler
     *
     * @return
     */
    public static Handler getHandler() {
        return mHandler;
    }


    /**
     * 项目MultiDex分包适配，多版本适配
     *
     * @return
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //        MultiDex.install(this);
    }


    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 2  * 获取版本号
     * 3  * @return 当前应用的版本号
     * 4
     */
    public static int getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }
}
